package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.model.entity.pojo.video.ShortHistory;
import com.lzw.breezeNovel.webApp.mapper.video.ShortHistoryMapper;
import com.lzw.breezeNovel.webApp.service.video.ShortHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/24
 * @description 观看历史
 */
@Service
@SuppressWarnings("all")
public class ShortHistoryServiceImpl implements ShortHistoryService {

    /**
     * 注入mapper
     */
    @Autowired
    private ShortHistoryMapper shortHistoryMapper;

    /**
     * 保存观看历史
     * @param shortId
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long shortId, Long userId) {
        //出现数据库判断是否已经观看过了
       ShortHistory shortHistory =  shortHistoryMapper.selectHistory(shortId,userId);
       //判断是否存在
        if(shortHistory == null){
            //不存在则保存
             shortHistory = new ShortHistory();
             shortHistory.setShortId(shortId);
             shortHistory.setUserId(userId);
             shortHistoryMapper.insert(shortHistory);
        }else
        {
            //存在则更新时间
             shortHistory.setUpdateTime(new Date());
             shortHistoryMapper.updateById(shortHistory);
        }
    }
}
